﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Calculator : Form
    {
        // Initilize needed variables.
        public static string sArg2;
        public static int? nEqual;
        public static bool resultplus;
        public static bool resultminus;
        public static bool resultdivide;
        public static bool resultmultiply;
        public static int? nArg1 = null;
        public static int? nArg2 = null;
        public static string sArg;
        public static bool nArg2Empty;
        public Calculator()
        {
            InitializeComponent();
        }

        // When a button is clicked add the respective number
        // to the contents of the CalcBox.
        private void button1_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 2;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 3;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 4;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 5;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 6;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 7;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 8;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 9;
        }
        private void button0_Click(object sender, EventArgs e)
        {
            CalcBox.Text = CalcBox.Text + 0;
        }
        // When +, -, *, or / is clicked it stores the contents of
        // CalcBox in nArg1. It then clears the CalcBox and stores
        // itself in sArg.
        private void buttonplus_Click(object sender, EventArgs e)
        {
            nArg2Empty = nArg2.Equals(null);
            if (nArg2Empty == false)
            {
                Calculator Calculator = new Calculator();
                Calculator.equal();
                nArg2 = Convert.ToInt32(nEqual);
                CalcBox.Text = Convert.ToString(nEqual);
                sArg = "+";
            }
            else
            {
                if (nArg2Empty == true)
                {
                    ConvertCalcBoxText();
                    sArg = "+";
                }
                else
                {
                    
                }
            }
        }

        private void buttonminus_Click(object sender, EventArgs e)
        {
            ConvertCalcBoxText();
            sArg = "-";
        }

        private void buttonmultiply_Click(object sender, EventArgs e)
        {
            ConvertCalcBoxText();
            sArg = "*";
        }

        private void buttondivide_Click(object sender, EventArgs e)
        {
            ConvertCalcBoxText();
            sArg = "/";
        }


        // Clears the CalcBox.
        private void buttonclear_Click(object sender, EventArgs e)
        {
            CalcBox.Text = "";
            nArg1 = null;
            nArg2 = null;
            nEqual = null;
            sArg = "";
        }
        public void buttonequals_Click(object sender, EventArgs e)
        {
            nArg2 = Convert.ToInt32(CalcBox.Text);
            Calculator Calculator = new Calculator();
            Calculator.equal();
            CalcBox.Text = Convert.ToString(nEqual);
        }

        private void CalcBox_TextChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Series of nested if loops to determine the desired operation and
        /// and then preform it.
        /// </summary>
        public void equal()
        {

            resultplus = sArg.Contains("+");
            if (resultplus == true)
            {
                nEqual = nArg1 + nArg2;
            }
            else
            {
                resultminus = sArg.Contains("-");
                if (resultminus == true)
                {
                    nEqual = nArg1 - nArg2;
                }
                else
                {
                    resultdivide = sArg.Contains("/");
                    if (resultdivide == true)
                    {
                        nEqual = nArg1 / nArg2;
                    }
                    else
                    {
                        resultmultiply = sArg.Contains("*");
                        if (resultmultiply == true)
                        {
                            nEqual = nArg1 * nArg2;
                        }
                        else
                        {

                        }
                    }
                }
            }
        }
        /// <summary>
        /// Converts the contents of the CalcBox and
        /// stores it in nArg1.
        /// </summary>
        private void ConvertCalcBoxText()
        {
            nArg1 = Convert.ToInt32(CalcBox.Text);
            CalcBox.Text = "";
        }
    }
}